#include <stdio.h>
#include <stdlib.h>
# include <string.h>
#include <sys/shm.h>
#include <sys/ipc.h>

// 使用共享内存进行通讯
#define PATH_NAME "."
#define PRO_ID  100
#define SZ 64

int main(int argc, char const *argv[])
{
    key_t key = ftok(PATH_NAME, PRO_ID);
    void *addr = NULL;
    if (key == -1) {
        perror("ftok error");
        exit(EXIT_FAILURE);
    }
    // 创建共享内存
    int shmid = shmget(key, SZ, IPC_CREAT | 0666);
    if (shmid == -1) {
        perror("shmget error");
        exit(EXIT_FAILURE);
    }
    printf("shmid = %d\n", shmid);

    // 将共享内存映射到进程的地址空间中
    addr = shmat(shmid, NULL, 0);
    if (addr == (void *)-1) {
        perror("shmat error");
        exit(EXIT_FAILURE);
    }
    memset(addr, 'A', 10);
    shmdt(addr);// 接触映射关系
    return 0;
}
